const noop = () => {}

// 需要直接点击，不能在异步函数之中
export function openSetting(name, callback = noop) {
  wx.getSetting({
    success({ authSetting }) {
      const key = `scope.${name}`
      // console.log('opensetting: ', key, authSetting[key] === false)
      let success = false
      if (authSetting[key] === false) {
        wx.openSetting({
          success({ authSetting }) {
            if (callback) {
              success = authSetting[key] !== false
              callback(success)
            }
          }
        })
      } else {
        // 已经有权限
        if (typeof callback === 'function') {
          callback(success)
        }
      }
    }
  })
}

// 获取用户授权信息
export function getSetting(callback) {
  return new Promise(resolve => {
    wx.getSetting({
      success({ authSetting }) {
        resolve(authSetting)
        if (callback) callback(authSetting)
      }
    })
  })
}

export default {
  open: openSetting,
  get: getSetting
}
